package practiceOne

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
)

type Monster struct {
	Name  string
	Age   int
	Skill string
}

func (this *Monster) Store() bool {
	data, err := json.Marshal(this)
	if err != nil {
		fmt.Println("序列化失败")
		return false
	}
	fmt.Printf("序列化成功,结果是%v", string(data))
	filePath := "D:\\WARRIOR\\Go\\go\\src\\ProjectOne\\advance\\UnitTest\\practiceOne\\01.txt"
	err2 := ioutil.WriteFile(filePath, data, 0666)
	if err2 != nil {
		fmt.Printf("写入文件错误%v", err2)
		return false
	}
	return true
}
func (this *Monster) Restore() bool {
	//先从文件中读取字符串
	filePath := "D:\\WARRIOR\\Go\\go\\src\\ProjectOne\\advance\\UnitTest\\practiceOne\\01.txt"
	data, err := ioutil.ReadFile(filePath)
	if err != nil {
		return false
	}
	err2 := json.Unmarshal(data, this)
	if err2 != nil {
		fmt.Println("反序列化失败")
		return false
	}
	return true
}
